package com.ky.manage.ui.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.StyleRes;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * 通用dialog
 *
 * @author zhaoyan
 * @date 2019/11/07.
 */

public class CommonDialog extends AbstractBaseDialog {

    private static final String KEY_DIALOG_LAYOUT_RES = "dialog_layout_res";
    private static final String KEY_DIALOG_WIDTH = "dialog_width";
    private static final String KEY_DIALOG_HEIGHT = "dialog_height";
    private static final String KEY_DIALOG_DIM = "dialog_dim";
    private static final String KEY_DIALOG_X = "dialog_x";
    private static final String KEY_DIALOG_Y = "dialog_y";
    private static final String KEY_DIALOG_CANCEL_OUTSIDE = "dialog_cancel_outside";
    private static final String KEY_DIALOG_GRAVITY = "dialog_gravity";
    private static final String KEY_DIALOG_ANIMATION_RES = "dialog_animation_res";
    private static final String KEY_DIALOG_THEME_STYLE_RES = "dialog_theme_style_res";
    private static final String STATE_SAVE_IS_HIDDEN = "state_save_is_hidden";

    private FragmentManager mFragmentManager;

    private boolean mIsCancelOutside = super.getCancelOutside();

    private String mTag = super.getFragmentTag();

    private float mDimAmount = super.getDimAmount();

    private boolean mKeyBackDismiss = super.getKeyBackDismiss();

    private int mDialogHeight = super.getDialogHeight();

    private int mDialogWidth = super.getDialogWidth();

    private int mGravity = super.getGravity();

    private int mDialogX = super.getDialogX();
    private int mDialogY = super.getDialogY();

    private OnDialogKeyBackListener mOnDialogKeyBackListener = super.getOnDialogKeyBackListener();

    /**
     * 弹窗动画
     */
    private int mDialogAnimationRes = super.getDialogAnimationRes();

    /**
     * 对话框style res
     */
    private int mDialogThemeStyleRes = super.getDialogThemeStyleRes();

    @LayoutRes
    private int mLayoutRes;


    /**
     * 直接使用传入进来的View,而不需要通过解析Xml
     */
    private View mDialogView;

    private ViewListener mViewListener;

    private DialogInterface.OnDismissListener mOnDismissListener;
    private Activity curActivity;

    public static CommonDialog create(FragmentManager manager) {
        CommonDialog dialog = new CommonDialog();
        dialog.setFragmentManager(manager);
        return dialog;
    }

    /**
     * 当设备旋转时,会重新调用onCreate,进行数据恢复
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mLayoutRes = savedInstanceState.getInt(KEY_DIALOG_LAYOUT_RES);
            mDialogWidth = savedInstanceState.getInt(KEY_DIALOG_WIDTH);
            mDialogHeight = savedInstanceState.getInt(KEY_DIALOG_HEIGHT);
            mDimAmount = savedInstanceState.getFloat(KEY_DIALOG_DIM);
            mDialogX = savedInstanceState.getInt(KEY_DIALOG_X);
            mDialogY = savedInstanceState.getInt(KEY_DIALOG_Y);
            mIsCancelOutside = savedInstanceState.getBoolean(KEY_DIALOG_CANCEL_OUTSIDE);
            mGravity = savedInstanceState.getInt(KEY_DIALOG_GRAVITY);
            mDialogAnimationRes = savedInstanceState.getInt(KEY_DIALOG_ANIMATION_RES);
            mDialogThemeStyleRes = savedInstanceState.getInt(KEY_DIALOG_THEME_STYLE_RES);
            //记录隐藏状态
            boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (isSupportHidden) {
                ft.hide(this);
            } else {
                ft.show(this);
            }
            ft.commitAllowingStateLoss();
        }
    }

    /**
     * 进行数据保存
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(KEY_DIALOG_LAYOUT_RES, mLayoutRes);
        outState.putInt(KEY_DIALOG_WIDTH, mDialogWidth);
        outState.putInt(KEY_DIALOG_HEIGHT, mDialogHeight);
        outState.putFloat(KEY_DIALOG_DIM, mDimAmount);
        outState.putInt(KEY_DIALOG_X, mDialogX);
        outState.putInt(KEY_DIALOG_Y, mDialogY);
        outState.putBoolean(KEY_DIALOG_CANCEL_OUTSIDE, mIsCancelOutside);
        outState.putInt(KEY_DIALOG_GRAVITY, mGravity);
        outState.putInt(KEY_DIALOG_ANIMATION_RES, mDialogAnimationRes);
        outState.putInt(KEY_DIALOG_THEME_STYLE_RES, mDialogThemeStyleRes);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
        super.onSaveInstanceState(outState);
    }

    @Override
    public void bindView(View v) {
        if (mViewListener != null) {
            mViewListener.onBindView(v, this);
        }
    }

    @Override
    protected View getDialogView() {
        return mDialogView;
    }

    /**
     * 直接传入控件
     *
     * @param view
     * @return
     */
    public CommonDialog setDialogView(View view) {
        mDialogView = view;
        return this;
    }

    @Override
    public int getLayoutRes() {
        return mLayoutRes;
    }

    /**
     * 传入弹窗xmL布局文件
     *
     * @param layoutRes
     * @return
     */
    public CommonDialog setLayoutRes(@LayoutRes int layoutRes) {
        mLayoutRes = layoutRes;
        return this;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mOnDismissListener != null) {
            mOnDismissListener.onDismiss(dialog);
        }
    }

    public CommonDialog setFragmentManager(FragmentManager manager) {
        mFragmentManager = manager;
        return this;
    }

    public CommonDialog setViewListener(ViewListener listener) {
        mViewListener = listener;
        return this;
    }

    public CommonDialog setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
        return this;
    }

    /**
     * 设置对话框style
     *
     * @param dialogThemeRes
     * @return
     */
    public CommonDialog setDialogThemeRes(@StyleRes int dialogThemeRes) {
        mDialogThemeStyleRes = dialogThemeRes;
        return this;
    }

    public CommonDialog setTag(String tag) {
        mTag = tag;
        return this;
    }

    public CommonDialog setOnKeyBackListener(OnDialogKeyBackListener onKeyBackListener) {
        mOnDialogKeyBackListener = onKeyBackListener;
        return this;
    }

    @Override
    public int getDialogX() {
        return mDialogX;
    }

    /**
     * 设置对话框x坐标
     *
     * @param dialogX
     */
    public CommonDialog setDialogX(int dialogX) {
        this.mDialogX = dialogX;
        return this;
    }

    @Override
    public int getDialogY() {
        return mDialogY;
    }

    /**
     * 设置对话框Y坐标
     *
     * @param dialogY
     */
    public CommonDialog setDialogY(int dialogY) {
        this.mDialogY = dialogY;
        return this;
    }

    @Override
    public float getDimAmount() {
        return mDimAmount;
    }

    /**
     * 设置弹窗背景透明度(0-1f)
     *
     * @param dim
     * @return
     */
    public CommonDialog setDimAmount(float dim) {
        mDimAmount = dim;
        return this;
    }

    @Override
    public boolean getKeyBackDismiss() {
        return mKeyBackDismiss;
    }

    /**
     * 设置点击返回是否消失
     *
     * @param keyBackDismiss
     * @return
     */
    public CommonDialog setKeyBackDismiss(boolean keyBackDismiss) {
        mKeyBackDismiss = keyBackDismiss;
        return this;
    }

    @Override
    public int getDialogHeight() {
        return mDialogHeight;
    }

    /**
     * 设置对话框高度
     *
     * @param heightPx
     * @return
     */
    public CommonDialog setDialogHeight(int heightPx) {
        mDialogHeight = heightPx;
        return this;
    }

    @Override
    public int getDialogWidth() {
        return mDialogWidth;
    }

    /**
     * 设置对话框宽度
     *
     * @param widthPx
     * @return
     */
    public CommonDialog setDialogWidth(int widthPx) {
        mDialogWidth = widthPx;
        return this;
    }

    @Override
    public int getGravity() {
        return mGravity;
    }

    /**
     * 设置对话框显示位置
     *
     * @param gravity
     * @return
     */
    public CommonDialog setGravity(int gravity) {
        mGravity = gravity;
        return this;
    }

    @Override
    public boolean getCancelOutside() {
        return mIsCancelOutside;
    }

    /**
     * 点击对话框外部是否可以关闭
     *
     * @param cancel
     * @return
     */
    public CommonDialog setCancelOutside(boolean cancel) {
        mIsCancelOutside = cancel;
        return this;
    }

    @Override
    public String getFragmentTag() {
        return mTag;
    }

    @Override
    protected int getDialogAnimationRes() {
        return mDialogAnimationRes;
    }

    /**
     * 设置弹窗动画
     *
     * @param animationRes
     * @return
     */
    public CommonDialog setDialogAnimationRes(@StyleRes int animationRes) {
        mDialogAnimationRes = animationRes;
        return this;
    }

    @Override
    public OnDialogKeyBackListener getOnDialogKeyBackListener() {
        return mOnDialogKeyBackListener;
    }

    public AbstractBaseDialog show() {
        try {
            show(mFragmentManager);
        } catch (Exception e) {
            //增加异常保护, dialog fragment 内部是commit 不是commitAllowingStateLoss
//            CustomLogUtils.d("zhaoyan", "CommonDialog show e = " + e.toString());
        }
        return this;
    }

    public Activity getCurActivity() {
        return curActivity;
    }

    public void setCurActivity(Activity curActivity) {
        this.curActivity = curActivity;
    }

    /**
     * 是否正在显示
     *
     * @return
     */
    public boolean isShowing() {
        return null != getDialog() && getDialog().isShowing();
    }

    public interface ViewListener {
        void onBindView(View v, AbstractBaseDialog byBaseDialog);
    }
}
